<?php 
class ControllerAccountPassword extends Controller {
	var $error = array();
	     
  	function index() {	
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('account_password'));

      		$response->redirect($url->ssl('account_login'));
    	}

		$language->load('controller/account_password.php');

    	$template->set('title', $language->get('heading_title'));
			  
    	if ($request->isPost() && $request->has('password', 'post') && $this->validate()) {
      		$sql = "update customer set password = '?' where customer_id = '?'";
      		$database->query($database->parse($sql, md5($request->gethtml('password', 'post')), $customer->getId()));
 
      		$session->set('message', $language->get('text_message'));
	  
	  		$response->redirect($url->ssl('account'));
    	}
	
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_password', $language->get('text_password'));

    	$view->set('entry_password', $language->get('entry_password'));
    	$view->set('entry_confirm', $language->get('entry_confirm'));

    	$view->set('button_continue', $language->get('button_continue'));
    	$view->set('button_back', $language->get('button_back'));
    
		$view->set('error_password', @$this->error['password']);
    	$view->set('error_confirm', @$this->error['confirm']);
	
    	$view->set('action', $url->ssl('account_password'));

    	$view->set('password', $request->gethtml('password', 'post'));

    	$view->set('confirm', $request->gethtml('confirm', 'post'));

    	$view->set('back', $url->ssl('account'));

		$template->set('content', $view->fetch('content/account_password.tpl'));
	
		$template->set($module->fetch());

    	$response->set($template->fetch('layout.tpl'));	
  	}
  
  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			
    	if ((strlen($request->gethtml('password', 'post')) < 4) || (strlen($request->gethtml('password', 'post')) > 20)) {
      		$this->error['password'] = $language->get('error_password');
    	}

    	if ($request->gethtml('confirm', 'post') != $request->gethtml('password', 'post')) {
      		$this->error['confirm'] = $language->get('error_confirm');
    	}  
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
}
?>
